{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "44a1811f",
   "metadata": {},
   "source": [
    "# 随机数组"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "577a874b",
   "metadata": {},
   "source": [
    "`NumPy`中的随机数组是通过子模块`numpy.random`实现的："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7a33df17",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "172a91ac",
   "metadata": {},
   "source": [
    "函数`np.random.rand()`可以用来生成0到1之间指定大小的的随机数组："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "16a2183c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.89610076, 0.20257692, 0.05524905],\n",
       "       [0.31842817, 0.37202256, 0.06250122]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.rand(2, 3)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "18c2ea82",
   "metadata": {},
   "source": [
    "函数`np.random.randn()`生成的则是服从标准正态分布的随机数组："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "315b4237",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.1460316 , -0.38227228, -0.57246999],\n",
       "       [ 1.64077195, -0.48344866,  1.78002085]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.randn(2, 3)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "3724894c",
   "metadata": {},
   "source": [
    "与标准模块`random`类似，`numpy.random`也有`choice()`函数，不过功能更强大。\n",
    "\n",
    "```python\n",
    "np.random.choice(a, size=None, replace=True, p=None)\n",
    "```\n",
    "\n",
    "该函数从一维数组或列表a中，随机选取出size大小形状的数组成数组。其中，replace参数表示选择的元素是否可重复，p是一个与a大小相同的数组，表示a中各个元素被选中的概率，默认为等概率：\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "65e920cf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 0, 2],\n",
       "       [0, 1, 1]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.choice(range(3), (2, 3))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b1d86316",
   "metadata": {},
   "source": [
    "`np.random.shuffle()`函数支持对数组的乱序操作："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "00c8a19b",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.arange(6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c8d8d123",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.random.shuffle(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "02fca772",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 1, 3, 5, 4, 0])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d68f08ee",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
